package com.hzqy.service.cast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.hzqy.commons.utils.JsonUtils;
import com.hzqy.commons.utils.StatusConvert;
import com.hzqy.persistence.dao.cast.CastDao;
import com.hzqy.persistence.dao.picture.PictureDao;
import com.hzqy.web.vo.CmCastVo;
import com.hzqy.web.vo.CmContentVo;
import com.hzqy.web.vo.CmPicturesVo;
@Service
public class CastServiceBean {
	private static final long serialVersionUID = 1L;
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	@Resource
	private CastDao castDao;
	@Resource
	private PictureDao pictureDao;
	public Object selectCastList(CmCastVo cmCastVo, int page, int rows) {
		Map<String,Object> map = new HashMap<String, Object>();
		Integer rowCount = castDao.findCastCount(cmCastVo);
		map.put("total", rowCount);
		if(rowCount > 0 && page > 0 && rows > 0) {
			map.put("firstResult", (page-1)*rows);
			map.put("maxResults", rows);
			map.put("f_cast_name", cmCastVo.getF_cast_name());
			map.put("f_cast_status", cmCastVo.getF_cast_status());
			map.put("f_cast_sex", cmCastVo.getF_cast_sex());
			map.put("f_cast_hometown", cmCastVo.getF_cast_hometown());
			List<CmCastVo> results = castDao.selectCastList(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<CmContentVo>());
		}
		return map;
	}
	public int insertCast(CmCastVo cmCastVo, String picJson) {
		int flag = 0;
		if(cmCastVo==null || cmCastVo.getF_cast_name()==null) {
			flag = -1;
		}
		if(flag ==0) {
			List<CmPicturesVo> picList = JsonUtils.jsonToPicVo(picJson);
			castDao.insertCast(cmCastVo);
			int f_cast_id = cmCastVo.getF_cast_id();
			for(int i=0;i<picList.size();i++) {
				CmPicturesVo cmPicturesVo = new CmPicturesVo();
				cmPicturesVo = picList.get(i);
				cmPicturesVo.setF_cp_relationid(f_cast_id);
				pictureDao.insertPic(cmPicturesVo);
			}
			return 1;
		}
		return flag;
	}
	public int updateCast(CmCastVo cmCastVo, String picJson,String picType) {
		int flag = 0;
		if(cmCastVo==null || cmCastVo.getF_cast_name()==null) {
			flag = -1;
		}
		if(flag ==0) {
			List<CmPicturesVo> picList = JsonUtils.jsonToPicVo(picJson);
			String listPicId = JsonUtils.jsonToPicIdList(picJson);
			int f_cv_relationid = cmCastVo.getF_cast_id();
			Map<String,Object> picMap = new HashMap<String, Object>();
			picMap.put("listPicId", listPicId);
			picMap.put("f_cp_relationid", f_cv_relationid);
			picMap.put("picType", picType);
			CmCastVo castVo =  castDao.selectCastVo(f_cv_relationid);
			castDao.updateCast(cmCastVo);
			pictureDao.deletePic(picMap);
			for(int i=0;i<picList.size();i++) {
				CmPicturesVo cmPicturesVo = new CmPicturesVo();
				cmPicturesVo = picList.get(i);
				cmPicturesVo.setF_cp_relationid(f_cv_relationid);
				pictureDao.insertPic(cmPicturesVo);
			}
			return 1;
		}
		return flag;
	}
	public int deleteCast(int f_cast_id) {
		return castDao.deleteCast(f_cast_id);
	}
	public CmCastVo selectCastVo(int f_cast_id) {
		return castDao.selectCastVo(f_cast_id);
	}
}
